const Subscription = require('egg').Subscription;

class TransferVideo extends Subscription {
    static get schedule() {
        return {
            cron: '0 23 * * *',
            type: 'all',
        };
    }

    async subscribe() {
        try {
            const arr = await this.ctx.service.topicLessonVideo.findVideosWithOriginalButNoStreamingLink();

            if (!Array.isArray(arr) || arr.length === 0) {
                return;
            }

            if (Array.isArray(arr) && arr.length > 0) {
                for (let i = 0; i < arr.length; i++) {
                    const item = arr[i];
                    await this.ctx.service.topicLessonVideo.updateStreaming(item.id);

                    const now = new Date();
                    const hour = now.getHours();
                    if (hour >= 6) {
                        console.log('It is past 8 AM, stopping the task.');
                        return;
                    }
                }
            } else {
                return
            }
        } catch (error) {
            console.error('Error occurred during video processing:', error);
        }
    }
}

module.exports = TransferVideo;